/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_fastdds/converter/WriteParamsConverter.hpp"
#include "dal_fastdds/converter/GuidConverter.hpp"

WriteParamsConverter* WriteParamsConverter::singleton_ = nullptr;

WriteParamsConverter::WriteParamsConverter() {}

WriteParamsConverter* WriteParamsConverter::GetInstance()
{
    if (singleton_ == nullptr) {
        singleton_ = new WriteParamsConverter();
    }
    return singleton_;
}

void WriteParamsConverter::ConvertWriteParamsFromDalToFastdds(dal_write_params_t* d_params,
                                                              WriteParams* f_params)
{
    GuidConverter::GetInstance()->ConvertGuidFromDalToFastdds(
        &(d_params->sample_identity.writer_guid), &(f_params->sample_identity().writer_guid()));
    f_params->sample_identity().sequence_number().high =
        d_params->sample_identity.sequence_number.high;
    f_params->sample_identity().sequence_number().low =
        d_params->sample_identity.sequence_number.low;

    GuidConverter::GetInstance()->ConvertGuidFromDalToFastdds(
        &(d_params->related_sample_identity.writer_guid),
        &(f_params->related_sample_identity().writer_guid()));
    f_params->related_sample_identity().sequence_number().high =
        d_params->related_sample_identity.sequence_number.high;
    f_params->related_sample_identity().sequence_number().low =
        d_params->related_sample_identity.sequence_number.low;
}

void WriteParamsConverter::ConvertWriteParamsFromFastddsToDal(WriteParams* f_params,
                                                              dal_write_params_t* d_params)
{
    GuidConverter::GetInstance()->ConvertGuidFromFastddsToDal(
        &(f_params->sample_identity().writer_guid()), &(d_params->sample_identity.writer_guid));
    d_params->sample_identity.sequence_number.high =
        f_params->sample_identity().sequence_number().high;
    d_params->sample_identity.sequence_number.low =
        f_params->sample_identity().sequence_number().low;

    GuidConverter::GetInstance()->ConvertGuidFromFastddsToDal(
        &(f_params->related_sample_identity().writer_guid()),
        &(d_params->related_sample_identity.writer_guid));
    d_params->related_sample_identity.sequence_number.high =
        f_params->related_sample_identity().sequence_number().high;
    d_params->related_sample_identity.sequence_number.low =
        f_params->related_sample_identity().sequence_number().low;
}
